################################################################################
################################################################################
### Script file to reproduce the illustrations from the Strategic Ambiguity project (JEPOP version)
### Victor Shin and Laron K. Williams
###
### Created: 6-11-24
### Modified: 
###
### Dependencies: 
### Output: Figure 1 in the manuscript
###
################################################################################


################################################################################
################################################################################

# Load the required packages
library(tidyverse)
library(ggplot2)
library(ggpubr)
library(truncnorm)
library(grid)

################################################################################

# Clear the space
rm(list = ls())

# Set theme of figures
personal_theme = theme_classic() + theme(axis.line.y = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank())

# Set the seed for reproducibility
set.seed(123)

### Panel 1:
y1 <- rtruncnorm(1000, a=0, b=10, mean = 5, sd = .5) |> round(digits = 0) 
y2 <- rtruncnorm(1000, a=0, b=10, mean = 5, sd = 1.5) |> round(digits = 0)
data1 <- data.frame(y1, y2)

p.1 <- pivot_longer(data1, c("y1", "y2"), names_to = "y", values_to = "val") |> 
  mutate(lbl = if_else(y == "y1", "Party A", "Party B")) |>
  ggplot(aes(val, fill=lbl)) + geom_histogram(alpha=0.5, binwidth=1,  position = "identity") +
  labs(title = "1", x= "", y="") + 
  scale_fill_manual(values = c("#F8766D", "#619CFF"), labels = c("Party A", "Party B")) +
  ylim(0, 700) + 
  scale_x_continuous(limits = c(0,10), breaks = c(0, 2.5, 5, 7.5, 10), labels = c('0', '2.5', '5', '7.5', '10')) + 
  personal_theme


### Panel 2:
y1 <- rtruncnorm(1000, a=0, b=10, mean = 4, sd = 2) |> round(digits = 0) 
y2 <- rtruncnorm(1000, a=0, b=10, mean = 6, sd = 2) |> round(digits = 0)
data2 <- data.frame(y1, y2)

p.2 <- pivot_longer(data2, c("y1", "y2"), names_to = "y", values_to = "val") |> 
  mutate(lbl = if_else(y == "y1", "Party A", "Party B")) |>
  ggplot(aes(val, fill=lbl)) + geom_histogram(alpha=0.5, binwidth=1,  position = "identity") +
  labs(title = "2", x= "", y="") + 
  scale_fill_manual(values = c("#F8766D", "#619CFF"), labels = c("Party A", "Party B")) +
  ylim(0, 700) + 
  scale_x_continuous(limits = c(0,10), breaks = c(0, 2.5, 5, 7.5, 10), labels = c('0', '2.5', '5', '7.5', '10')) + 
  personal_theme


### Panel 3:
y1 <- rtruncnorm(1000, a=0, b=10, mean = 2, sd = 2) |> round(digits = 0) 
y2 <- rtruncnorm(1000, a=0, b=10, mean = 8, sd = 2) |> round(digits = 0)
data3 <- data.frame(y1, y2)

p.3 <- pivot_longer(data3, c("y1", "y2"), names_to = "y", values_to = "val") |> 
  mutate(lbl = if_else(y == "y1", "Party A", "Party B")) |>
  ggplot(aes(val, fill=lbl)) + geom_histogram(alpha=0.5, binwidth=1,  position = "identity") +
  labs(title = "3", x= "", y="") + 
  scale_fill_manual(values = c("#F8766D", "#619CFF"), labels = c("Party A", "Party B")) +
  ylim(0, 700) + 
  scale_x_continuous(limits = c(0,10), breaks = c(0, 2.5, 5, 7.5, 10), labels = c('0', '2.5', '5', '7.5', '10')) + 
  personal_theme


### Panel 4:
y1 <- rtruncnorm(1000, a=0, b=10, mean = 4, sd = .5) |> round(digits = 0) 
y2 <- rtruncnorm(1000, a=0, b=10, mean = 6, sd = .5) |> round(digits = 0)
data4 <- data.frame(y1, y2)

p.4 <- pivot_longer(data4, c("y1", "y2"), names_to = "y", values_to = "val") |> 
  mutate(lbl = if_else(y == "y1", "Party A", "Party B")) |>
  ggplot(aes(val, fill=lbl)) + geom_histogram(alpha=0.5, binwidth=1,  position = "identity") +
  labs(title = "4", x= "", y="") + 
  scale_fill_manual(values = c("#F8766D", "#619CFF"), labels = c("Party A", "Party B")) +
  ylim(0, 700) + 
  scale_x_continuous(limits = c(0,10), breaks = c(0, 2.5, 5, 7.5, 10), labels = c('0', '2.5', '5', '7.5', '10')) + 
  personal_theme


### Panel 5:
y1 <- rtruncnorm(1000, a=0, b=10, mean = 4, sd = 3) |> round(digits = 0) 
y2 <- rtruncnorm(1000, a=0, b=10, mean = 6, sd = 3) |> round(digits = 0)
data5 <- data.frame(y1, y2)

p.5 <- pivot_longer(data5, c("y1", "y2"), names_to = "y", values_to = "val") |> 
  mutate(lbl = if_else(y == "y1", "Party A", "Party B")) |>
  ggplot(aes(val, fill=lbl)) + geom_histogram(alpha=0.5, binwidth=1,  position = "identity") +
  labs(title = "5", x= "", y="") + 
  scale_fill_manual(values = c("#F8766D", "#619CFF"), labels = c("Party A", "Party B")) +
  ylim(0, 700) + 
  scale_x_continuous(limits = c(0,10), breaks = c(0, 2.5, 5, 7.5, 10), labels = c('0', '2.5', '5', '7.5', '10')) + 
  personal_theme


### Panel 6:
y1 <- rtruncnorm(1000, a=0, b=10, mean = 4, sd = 1.3) |> round(digits = 0) 
y2 <- rtruncnorm(1000, a=0, b=10, mean = 6, sd = 1.3) |> round(digits = 0)
data6 <- data.frame(y1, y2)

p.6 <- pivot_longer(data6, c("y1", "y2"), names_to = "y", values_to = "val") |> 
  mutate(lbl = if_else(y == "y1", "Party A", "Party B")) |>
  ggplot(aes(val, fill=lbl)) + geom_histogram(alpha=0.5, binwidth=1,  position = "identity") +
  labs(title = "6", x= "", y="") + 
  scale_fill_manual(values = c("#F8766D", "#619CFF"), labels = c("Party A", "Party B")) +
  ylim(0, 700) + 
  scale_x_continuous(limits = c(0,10), breaks = c(0, 2.5, 5, 7.5, 10), labels = c('0', '2.5', '5', '7.5', '10')) + 
  personal_theme


### Panel 7:
y1 <- rtruncnorm(1000, a=0, b=10, mean = 3, sd = 3) |> round(digits = 0) 
y2 <- rtruncnorm(1000, a=0, b=10, mean = 5, sd = 0.5) |> round(digits = 0)
y3 <- rtruncnorm(1000, a=0, b=10, mean = 7, sd = 1) |> round(digits = 0)
data7 <- data.frame(y1, y2, y3)

p.7 <- pivot_longer(data7, c("y1", "y2", "y3"), names_to = "y", values_to = "val") |> 
  mutate(lbl = case_when(y == "y1" ~ "Party A",
                         y == "y2" ~ "Party B",
                         y == "y3" ~ "Party C")) |>
  ggplot(aes(val, fill=lbl)) + geom_histogram(alpha=0.5, binwidth=1,  position = "identity") +
  labs(title = "7", x= "", y="") + 
  scale_fill_manual(values = c("#F8766D", "#619CFF", "#00BA38"), labels = c("Party A", "Party B", "Party C")) +
  ylim(0, 700) + 
  scale_x_continuous(limits = c(0,10), breaks = c(0, 2.5, 5, 7.5, 10), labels = c('0', '2.5', '5', '7.5', '10')) + 
  personal_theme


### Panel 8:
y1 <- rtruncnorm(1000, a=0, b=10, mean = 2, sd = .5) |> round(digits = 0) 
y2 <- rtruncnorm(1000, a=0, b=10, mean = 5, sd = .5) |> round(digits = 0)
y3 <- rtruncnorm(1000, a=0, b=10, mean = 8, sd = .5) |> round(digits = 0)
data8 <- data.frame(y1, y2, y3)

p.8 <- pivot_longer(data8, c("y1", "y2", "y3"), names_to = "y", values_to = "val") |> 
  mutate(lbl = case_when(y == "y1" ~ "Party A",
                         y == "y2" ~ "Party B",
                         y == "y3" ~ "Party C")) |>
  ggplot(aes(val, fill=lbl)) + geom_histogram(alpha=0.5, binwidth=1,  position = "identity") +
  labs(title = "8", x= "", y="") + 
  scale_fill_manual(values = c("#F8766D", "#619CFF", "#00BA38"), labels = c("Party A", "Party B", "Party C")) +
  ylim(0, 700) + 
  scale_x_continuous(limits = c(0,10), breaks = c(0, 2.5, 5, 7.5, 10), labels = c('0', '2.5', '5', '7.5', '10')) + 
  personal_theme


### Label the parties by color
p.labs <- ggplot() + 
  theme_void() + 
  annotate("text", x = 1:3, y = 1, label = c('Party A', 'Party B', 'Party C'), colour = c("#F8766D", "#619CFF", "#00BA38"), size = 7) +
  xlim(0,4) + ylim(1,1) + 
  theme(plot.margin = margin(0,0,0,0))
p.labs


### Combine the plots
top <- ggarrange(p.1, p.2, p.3, p.4, p.5, p.6, p.7, p.8,
                         label.x = 0,
                         legend="none",
                         ncol=2,
                         nrow=4,
                         common.legend = TRUE)

fig.ill <- ggarrange(top, p.labs,
                 ncol=1,
                 nrow=2,
                 heights = c(10,1))

### Save the plot
pdf("illustrations.pdf")
fig.ill
dev.off()




















